<template>
	<view>
		<view class="carousel">
			<swiper indicator-dots circular=true duration="400">
				<swiper-item class="swiper-item" v-for="(item,index) in imgList" :key="index">
					<view class="image-wrapper">
						<image :src="item" class="loaded" mode="aspectFill" @click="previewImage(item)"></image>
					</view>
				</swiper-item>
			</swiper>
		</view>

		<view class="introduce-section">
			<view class="price-box">
				<text class="price-tip">¥</text>
				<text class="price">{{product.currentPrice}}</text>
				<!-- <text class="m-price">¥488</text>
				<text class="coupon-tip">7折</text> -->
			</view>
			<text class="title">{{product.name}}</text>
			<view class="bot-row">
				<text>{{product.goodsSellingPoint}}</text>
			</view>
		</view>

		<view class="c-list">
			<view class="c-row b-b">
				<text class="tit">优惠券</text>
				<text class="con t-r red"></text>
				<text class="yticon icon-you"></text>
			</view>
			<view class="c-row b-b">
				<text class="tit">促销活动</text>
				<view class="con-list">
					<text>新人首单送20元无门槛代金券</text>
					<text>订单满50减10</text>
					<text>订单满100减30</text>
					<text>单笔购买满两件免邮费</text>
				</view>
			</view>
			<view class="c-row b-b">
				<text class="tit">服务</text>
				<view class="bz-list con">
					<text>7天无理由退换货 ·</text>
					<text>假一赔十 ·</text>
				</view>
			</view>
		</view>

		<view class="eva-section">
			<view class="e-header">
				<text class="tit">评价</text>
				<text>({{evaluateMap.goodsEvaDtos.length > 0 ? evaluateMap.goodsEvaDtos.length:0}})</text>
				<text class="tip">更多</text>
				<text class="yticon icon-you"></text>
			</view>
			<view class="eva-box">
				<image class="portrait" :src="evaluateMap.goodsEvaDtos[0].userIcon" mode="aspectFill"></image>
				<view class="right">
					<text class="name">{{evaluateMap.goodsEvaDtos[0].userName}}</text>
					<text class="con">{{evaluateMap.goodsEvaDtos[0].content}}</text>
					<view class="bot" v-if="evaluateMap.goodsEvaDtos.length > 0">
						<text class="attr">买家评价：{{evaluateMap.goodsEvaDtos[0].startNum}}颗星</text>
						<text class="time">{{evaluateMap.goodsEvaDtos[0].addTime}}</text>
					</view>
				</view>
			</view>
		</view>

		<view class="store-section">
			<image :src="product.storeLogoUrl"></image>
			<view class="store">
				<text class="name">{{product.storeName}}</text>
				<text class="product">商品数： {{storeGoodsNum}}</text>
			</view>
			<text class="yticon icon-you"></text>
		</view>

		<view class="detail-desc">
			<view class="d-header">
				<text>图文详情</text>
			</view>
			<rich-text :nodes="desc"></rich-text>
		</view>

		<view class="goods-nav">
			<uni-goods-nav :fill="true" :options="options" :buttonGroup="buttonGroup" @click="onClick"
				@buttonClick="buttonClick" />
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				id: '',
				imgList: [],
				product: {},
				evaluateMap: {},
				storeGoodsNum: 0,
				desc: '',
				options: [{
					icon: 'headphones',
					text: '客服'
				}, {
					icon: 'shop',
					text: '店铺',
				}, {
					icon: 'cart',
					text: '购物车',
					infoBackgroundColor: '#ff0000',
					info: 0
				}],
				buttonGroup: [{
						text: '加入购物车',
						backgroundColor: '#ff0000',
						color: '#fff'
					},
					{
						text: '立即购买',
						backgroundColor: '#ffa200',
						color: '#fff'
					}
				]
			}
		},
		onLoad(e) {
			this.id = e.id;

			uni.showLoading({
				title: '加载中...',
				mask: false
			});
			this.loadData();
			this.getCartSize();
		},
		methods: {
			async loadData() {
				let data = {
					"id": this.id,
					"goodsGsp": ""
				}
				
				let res = await this.helper.requestAPI(this.api.goodsdetail,data);
				let product = res.body;
				this.imgList.push(product.goods.fullPath);
				product.goods.goodsPhotos.forEach(item => {
					this.imgList.push(item.qiniuUrl);
				});
				this.product = product.goods;
				this.evaluateMap = product.goodsEvaluateMap;
				this.storeGoodsNum = product.storeGoodsNum;
				
				//商品详情适配
				let desc = product.goods.goodsDetails;
				if (desc.indexOf("width='980px' height='auto'") > 0) {
					this.desc = desc.replace(/width='980px' height='auto'/g,
						"style='width:100%;height:auto'");
				} else {
					this.desc = desc.replace(/img/g, "img style='width:100%;height:auto'");
				}
			},
			async getCartSize() {
				let res = await this.helper.requestAPI(this.api.cartsize);
				let cartSize = res.body.cartSize;
				let option = this.options[2];
				option.info = cartSize;
				
			},
			onClick(e) {
				uni.showToast({
					title: `点击${e.content.text}`,
					icon: 'none'
				})

				if (e.index == 2) {
					uni.switchTab({
						url: '../cart/cart'
					})
				}
			},
			buttonClick(e) {
				console.log(e)

				let goodsStatus = this.product.goodsStatus;
				if (goodsStatus == 1) {
					this.$api.msg('商品已下架');
					return;
				}

				if (e.index == 0) {
					//添加购物车
					this.addCart(this.id);
				} else {
					//提交订单
					uni.navigateTo({
						url: '/pages/order/createOrder?goodsId=' + this.id
					});
				}

			},
			previewImage(item) {
				uni.previewImage({
					current: item,
					count: this.imgList.count,
					loop: true,
					urls: this.imgList
				})
			},
			async addCart(goodsId) {
				let data = {
					"goodsId": goodsId,
					"count": "1"
				}
				
				let res = await this.helper.requestAPI(this.api.cartadd,data);
				this.$api.msg('添加成功');
				
				let cartSize = res.data.body.cartSize;
				let option = this.options[2];
				option.info = cartSize;
			}
		}
	}
</script>

<style lang="scss">
	page {
		background: $page-color-base;
		padding-bottom: 160rpx;
	}

	.carousel {
		height: 722rpx;
		position: relative;

		swiper {
			height: 100%;
		}

		.image-wrapper {
			width: 100%;
			height: 100%;
		}

		.swiper-item {
			display: flex;
			justify-content: center;
			align-content: center;
			height: 750rpx;
			overflow: hidden;

			image {
				width: 100%;
				height: 100%;
			}
		}

	}

	.introduce-section {
		background: #fff;
		padding: 20rpx 30rpx;

		.title {
			font-size: 26rpx;
			color: $font-color-dark;
			height: 26rpx;
			line-height: 26rpx;
		}

		.price-box {
			display: flex;
			align-items: baseline;
			height: 60rpx;
			padding: 10rpx 0;
			font-size: 26rpx;
			color: $uni-color-primary;
		}

		.price {
			font-size: $uni-font-size-lg + 4rpx;
		}

		.m-price {
			margin: 0 12rpx;
			color: $font-color-light;
			text-decoration: line-through;
		}

		.coupon-tip {
			align-items: center;
			padding: 4rpx 10rpx;
			background: $uni-color-primary;
			font-size: $uni-font-size-sm;
			color: #fff;
			border-radius: 6rpx;
			line-height: 1;
			transform: translateY(-4rpx);
		}

		.bot-row {
			display: flex;
			align-items: center;
			height: 50rpx;
			font-size: $uni-font-size-sm;
			color: $font-color-light;

			text {
				flex: 1;
			}
		}
	}

	.c-list {
		font-size: $uni-font-size-sm + 2rpx;
		color: $font-color-base;
		background: #fff;

		.c-row {
			display: flex;
			align-items: center;
			padding: 20rpx 30rpx;
			position: relative;
		}

		.tit {
			width: 140rpx;
		}

		.con {
			flex: 1;
			color: $font-color-dark;

			.selected-text {
				margin-right: 10rpx;
			}
		}

		.bz-list {
			height: 40rpx;
			font-size: $uni-font-size-sm+2rpx;
			color: $font-color-dark;

			text {
				display: inline-block;
				margin-right: 30rpx;
			}
		}

		.con-list {
			flex: 1;
			display: flex;
			flex-direction: column;
			color: $font-color-dark;
			line-height: 40rpx;
		}

		.red {
			color: $uni-color-primary;
		}
	}

	.eva-section {
		display: flex;
		flex-direction: column;
		padding: 20rpx 30rpx;
		background: #fff;
		margin-top: 16rpx;

		.e-header {
			display: flex;
			align-items: center;
			height: 70rpx;
			font-size: $uni-font-size-sm + 2rpx;
			color: $font-color-light;

			.tit {
				font-size: $uni-font-size-base + 2rpx;
				color: $font-color-dark;
				margin-right: 4rpx;
			}

			.tip {
				flex: 1;
				text-align: right;
			}

			.icon-you {
				margin-left: 10rpx;
			}
		}
	}

	.eva-box {
		display: flex;
		padding: 20rpx 0;

		.portrait {
			flex-shrink: 0;
			width: 80rpx;
			height: 80rpx;
			border-radius: 50%;
		}

		.right {
			flex: 1;
			display: flex;
			flex-direction: column;
			font-size: $uni-font-size-base;
			color: $font-color-base;
			padding-left: 26rpx;

			.con {
				font-size: $uni-font-size-base;
				color: $font-color-dark;
				padding: 20rpx 0;
			}

			.bot {
				display: flex;
				justify-content: space-between;
				font-size: $uni-font-size-sm;
				color: $font-color-light;
			}
		}
	}

	.store-section {
		display: flex;
		align-items: center;
		padding: 20rpx 20rpx;
		background: white;
		margin-top: 16rpx;

		image {
			height: 80rpx;
			width: 80rpx;
		}

		.store {
			display: flex;
			flex-direction: column;
			flex: 1;
			padding-left: 20rpx;

			.name {
				color: $font-color-dark;
				font-size: $uni-font-size-base;
			}

			.product {
				color: $font-color-light;
				font-size: $uni-font-size-sm - 2rpx;
			}
		}
	}

	.detail-desc {
		background: #fff;
		margin-top: 16rpx;

		.d-header {
			display: flex;
			justify-content: center;
			align-items: center;
			height: 80rpx;
			font-size: $uni-font-size-base + 2rpx;
			color: $font-color-dark;
			position: relative;

			text {
				padding: 0 20rpx;
				background: #fff;
				position: relative;
				z-index: 1;
			}

			&:after {
				position: absolute;
				left: 50%;
				top: 50%;
				transform: translateX(-50%);
				width: 300rpx;
				height: 0;
				content: '';
				border-bottom: 1rpx solid #ccc;
			}
		}
	}

	.goods-nav {
		display: flex;
		flex-direction: column;
		position: fixed;
		left: 0;
		right: 0;
		bottom: 0;
		z-index: 99;
		background-color: white;
		
		padding-bottom: env(safe-area-inset-bottom); 
	}
</style>
